package com.cuijian.demo.common;

import java.util.HashMap;
import java.util.Map;

public class JsonResult {

    private int code;
    private boolean success;
    private String message;
    private Object data;

    public JsonResult(int code, boolean success, String message, Object data) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = data;
    }

    // 成功响应
    public static JsonResult success() {
        return new JsonResult(200, true, "操作成功", null);
    }

    public static JsonResult success(Object data) {
        return new JsonResult(200, true, "操作成功", data);
    }

    public static JsonResult success(String message, Object data) {
        return new JsonResult(200, true, message, data);
    }

    public static JsonResult success(String message) {
        return new JsonResult(200, true, message, null);
    }

    // 错误响应
    public static JsonResult error(String message) {
        return new JsonResult(500, false, message, null);
    }

    public static JsonResult error(String message, Object data) {
        return new JsonResult(500, false, message, data);
    }

    public static JsonResult error(int code, String message) {
        return new JsonResult(code, false, message, null);
    }

    public static JsonResult error(int code, String message, Object data) {
        return new JsonResult(code, false, message, data);
    }

    // 自定义状态码
    public static JsonResult of(int code, boolean success, String message, Object data) {
        return new JsonResult(code, success, message, data);
    }

    // 常用状态码
    public static JsonResult badRequest(String message) {
        return new JsonResult(400, false, message, null);
    }

    public static JsonResult unauthorized(String message) {
        return new JsonResult(401, false, message, null);
    }

    public static JsonResult forbidden(String message) {
        return new JsonResult(403, false, message, null);
    }

    public static JsonResult notFound(String message) {
        return new JsonResult(404, false, message, null);
    }

    // getter and setter
    public int getCode() { return code; }
    public void setCode(int code) { this.code = code; }

    public boolean isSuccess() { return success; }
    public void setSuccess(boolean success) { this.success = success; }

    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }

    public Object getData() { return data; }
    public void setData(Object data) { this.data = data; }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("success", success);
        map.put("message", message);
        map.put("data", data);
        return map;
    }
}